import {checkPhone, handlerGobackClick, hint} from "../../../utils/util";
import User from "../../../models/user";
import Dialog from '@vant/weapp/dialog/dialog';
const user = new User()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    codeText:"获取验证码",
    isDisabled:false,
    authCode:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  inputPhone(e){
    this.setData({
      phone: e.detail.value
    })
  },
  inputAuthCode(e){
    this.setData({
      authCode: parseInt(e.detail.value)
    })
  },
  handlerGobackClick,
  pwNext(){
    let ownerPhone = checkPhone(this.data.phone)
    if (ownerPhone !== true) {
      hint(ownerPhone)
      return
    }
    if(!this.data.authCode){
      hint("验证码不能为空")
      return;
    }
    user.appCheckCode(this.data.phone,this.data.authCode)
        .then((res)=>{
          user.editPhone(this.data.phone)
              .then((res)=>{
                let status = res.result.registerStatus
                if(status === 1000){
                  user.logout()
                      .then((res)=>{
                        if(res.success){
                          wx.clearStorage({
                            success:()=>{
                              wx.reLaunch({
                                url:'/pages/login-before/index'
                              })
                            },
                            fail:()=>{
                              hint("登出失败")
                            }
                          })
                        }
                      })
                      .catch((err)=>{
                        console.log(err);})
                  return
                }
                if(status === 1){
                  Dialog.alert({
                    closeOnClickOverlay:true,
                    message: '该手机号已被注册',
                  }).then(() => {
                    // on close
                  });
                  return;
                }
              })
              .catch((err)=>{
                console.log(err);})
        })
        .catch((err)=>{
          console.log(err);})
  },
  getCode(){
    let ownerPhone = checkPhone(this.data.phone)
    if (ownerPhone !== true) {
      hint(ownerPhone)
      return
    }
    if(!this.data.isDisabled){
      user.registerGetAuthCode(this.data.phone,3)
          .then((res)=>{
              this.setTime()})
          .catch((err)=>{
            console.log(err);})
    }else {
      wx.showToast({title:"请勿频繁点击",icon:'none',duration:2000})
    }
  },
  setTime(){
    let time = 60;
    let i = 0;
    let that = this;
    //1.在60秒之内，button上倒计时60秒，并显示
    //2.向controller请求验证码，并发送到指定的手机号上面
    let set = setInterval(()=> {
      this.setData({
        codeText:(--time)+"秒"
      })
      i++;
      //此变量用于标识只调用一次获取验证码的函数
      if (i==1) {
        //使获取验证码按钮不可点击
        this.setData({
          isDisabled:true
        })
      }
    }, 1000);

    //60秒之后需要做的事情
    setTimeout(()=> {
      //恢复获取验证码按钮可点击
      this.setData({
        isDisabled:false,
        codeText:"获取验证码",
      })
      clearInterval(set);//清除计时器
    }, time * 1000); //60000
  }
})
